﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using System.Reflection;

namespace Snake_Classic
{
    public partial class HightScoreForm : Form
    {
        public HightScoreForm()
        {
            InitializeComponent();
            
        }
        MenuForm menu;
        private void HightScoreForm_Load(object sender, EventArgs e)
        {
            ReadXMLDocument();
        }


        private void ReadXMLDocument()
        {
            int count = 1;
            string pname = string.Empty, pscore = string.Empty;
            Dictionary<string, int> scoreDictionary = new Dictionary<string, int>();
            string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string Filepath = Path.Combine(directory, "Score.xml"); 
            XmlDocument xd = new XmlDocument();
            FileStream fs = new FileStream(Filepath, FileMode.Open);
            xd.Load(fs);
            XmlNodeList list = xd.GetElementsByTagName("Score");   
            
            for (int i = 0; i < xd.DocumentElement.ChildNodes.Count; i++)
            {  
                XmlElement name = (XmlElement)xd.GetElementsByTagName("name")[i];       
                XmlElement score = (XmlElement)xd.GetElementsByTagName("score")[i];
                
                //set variables from tegs
                pname = name.InnerText;
                pscore = score.InnerText;
                scoreDictionary.Add(pname, Int32.Parse(pscore));                                 
            }
            // Display results.
            var items = from pair in scoreDictionary orderby pair.Value descending select pair;
            //array of labels, for view a player name and their score
            Label[] numberLabelArray = new Label[] { label5, label6, label7, label8, label9, label10, label11, label12, label13, label14};
            Label[] naneLabelArray = new Label[] { label15, label16, label17, label18, label19, label20, label21, label22, label23, label24};
            Label[] scoreLabelArray = new Label[] { label25, label26, label27, label28, label29, label30, label31, label32, label33, label34};
            foreach (KeyValuePair<string, int> pair in items)
            {
                numberLabelArray[count - 1].Text = count.ToString();
                naneLabelArray[count - 1].Text = pair.Key.ToString();
                scoreLabelArray[count - 1].Text = pair.Value.ToString();
                count++;
                if (count == 11)
                {
                    break;
                }
            }
                // Close file stream 
                fs.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
            menu = new MenuForm();
            menu.Show();
        }
    }
}
